
/*
'===============================================================================
'  MP.LayerHelper.MP_OBS.BL.AspnetUsersManager
'===============================================================================
*/

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.FactoryClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using MP.Core.Library;

namespace MP.LayerHelper.MP_OBS.BLL
{
	public class AspnetUsersManager : AspnetUsersManagerBase
	{
		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		public AspnetUsersManager()
		{
			// Nothing for now.
		}
		
		/// <summary>
		/// Method to create an instance of AspnetUsersManager
		/// </summary>
		/// <returns>An instant of AspnetUsersManager class</returns>
		public static AspnetUsersManager CreateInstant()
		{
			return new AspnetUsersManager();
		}
        public DataTable GetAll(string branchId, string departId)
        {
            return GetAll(branchId, departId, "", "");
        }
        public bool UpdateUserSub(string username, string loweredUser)
        {
            string strSQL = "update aspnet_Users set UserName=@UserName,LoweredUserName=@LoweredUserName where UserName=@UserName";
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@UserName", username);
            param[1] = new SqlParameter("@LoweredUserName", loweredUser);
            int result=SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringOBS, CommandType.Text, strSQL);
            if (result > 0)
                return true;
            return false;
        }
        public DataTable GetAll(string branchId, string departId, string users, string agentnumber)
        {
            string strSQL = @"Select u.*,m.IsApproved,m.IsLockedOut,b.BranhCode,d.DepartCode, j.JobName, e.AgentNumber,e.MachineId
                            From ((((((aspnet_Users u INNER JOIN aspnet_Membership m ON m.UserId=u.UserId) 
		                         LEFT JOIN dbo.hrPosition p ON u.UserName=p.UserId and p.IsMaster=1 and p.IsDeleted=0)
		                         LEFT JOIN dbo.hrHumanOrganize o ON p.OrganizeId=o.Id and o.IsDeleted=0)
                                 LEFT JOIN dbo.hrBranch b ON o.BranchId=b.Id and b.IsDeleted=0)
                                 LEFT JOIN dbo.maDepartment d ON o.DepartId=d.Id and d.IsDeleted=0)
                                 LEFT JOIN dbo.hrJobTitle j ON o.JobId=j.Id and j.IsDeleted=0)
                                 LEFT JOIN dbo.hrEmployee e ON u.username=e.WindowsUser and e.IsDeleted=0
                            Where u.IsDeleted=0 {0}
                            Order By b.BranhCode,d.DepartCode,j.Priority,j.JobName,u.UserName";
            string subSQL = "";
            if (!string.IsNullOrEmpty(users) && !string.IsNullOrEmpty(agentnumber))
            {
                subSQL += " and (u.username IN (" + FString.SplitSearchString(users.Replace("'", "")) + ") OR " +
                          "      e.AgentNumber IN (" + FString.SplitSearchString(agentnumber.Replace("'", "")) + ")) ";
            }
            else
            {
                if (!string.IsNullOrEmpty(users))
                {
                    subSQL += " and u.username IN (" + FString.SplitSearchString(users.Replace("'", "")) + ") ";
                }
                if (!string.IsNullOrEmpty(agentnumber))
                {
                    subSQL += " and e.AgentNumber IN (" + FString.SplitSearchString(agentnumber.Replace("'", "")) + ") ";
                }
            }
            if (!string.IsNullOrEmpty(branchId) && Util.IsGuid(branchId))
            {
                subSQL += " and b.Id='" + branchId + "' ";
            }
            if (!string.IsNullOrEmpty(departId) && Util.IsGuid(departId))
            {
                subSQL += " and d.Id='" + departId + "' ";
            }
            strSQL = string.Format(strSQL, subSQL);
            return SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringOBS, CommandType.Text, strSQL);
        }

        public AspnetUsersEntity GetByUserName(string UserName)
        {
            EntityCollection<AspnetUsersEntity> items = new EntityCollection<AspnetUsersEntity>();

            IPredicateExpression predicate = new PredicateExpression();
            predicate.Add(AspnetUsersFields.UserName == UserName);
            predicate.Add(AspnetUsersFields.IsDeleted == false);

            RelationPredicateBucket filter = new RelationPredicateBucket();
            filter.PredicateExpression.Add(predicate);

            using (DataAccessAdapterBase adapter = new DataAccessAdapterFactory().CreateAdapter())
            {
                adapter.FetchEntityCollection(items, filter);
            }

            if (items != null && items.Count > 0)
                return items[0];
            return null;
        }

        public AspnetUsersEntity GetByUserId(string UserId)
        {
            EntityCollection<AspnetUsersEntity> items = new EntityCollection<AspnetUsersEntity>();

            IPredicateExpression predicate = new PredicateExpression();
            predicate.Add(AspnetUsersFields.UserId == UserId);
            predicate.Add(AspnetUsersFields.IsDeleted == false);

            RelationPredicateBucket filter = new RelationPredicateBucket();
            filter.PredicateExpression.Add(predicate);

            using (DataAccessAdapterBase adapter = new DataAccessAdapterFactory().CreateAdapter())
            {
                adapter.FetchEntityCollection(items, filter);
            }

            if (items != null && items.Count > 0)
                return items[0];
            return null;
        }

        public void UpdateUser(string userold, string usernew)
        {
            AspnetUsersEntity user = GetByUserName(userold);
            if (user != null)
            {
                user.UserName = usernew;
                user.LoweredUserName = usernew.ToLower();
                Update(user);
            }
        }
	}
}
